package com.huawei.hwid.core.encrypt;

import android.content.Context;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.Base64;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.g;
import com.huawei.hwid.core.d.m;
import com.huawei.secure.android.common.encrypt.rsa.RSAEncrypt;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class c {
    public static String a(Context context, String str) {
        String stringBuffer = new StringBuffer().append(HwAccountConstants.ENCODE_STR).append(AES128_ECB.PART_CODE_KEY).append(Hex.PART_CODE).append(m.d("562.")).toString();
        String str2 = "";
        try {
            byte[] decode = AES128_ECB_HEX.decode(str, b.d(stringBuffer), 0);
            if (decode != null) {
                stringBuffer = new String(decode, "UTF-8");
                try {
                    Arrays.fill(decode, (byte) 0);
                    return stringBuffer;
                } catch (InvalidKeyException e) {
                    str2 = stringBuffer;
                    e.d("HwIdEncrypter", "InvalidKeyException", true);
                    if (TextUtils.isEmpty(str2)) {
                        return str2;
                    }
                    return c(context, str);
                } catch (BadPaddingException e2) {
                    str2 = stringBuffer;
                    e.d("HwIdEncrypter", "BadPaddingException", true);
                    if (TextUtils.isEmpty(str2)) {
                        return c(context, str);
                    }
                    return str2;
                } catch (IllegalBlockSizeException e3) {
                    str2 = stringBuffer;
                    e.d("HwIdEncrypter", "IllegalBlockSizeException", true);
                    if (TextUtils.isEmpty(str2)) {
                        return str2;
                    }
                    return c(context, str);
                } catch (NoSuchAlgorithmException e4) {
                    str2 = stringBuffer;
                    e.d("HwIdEncrypter", "NoSuchAlgorithmException", true);
                    if (TextUtils.isEmpty(str2)) {
                        return c(context, str);
                    }
                    return str2;
                } catch (NoSuchPaddingException e5) {
                    str2 = stringBuffer;
                    e.d("HwIdEncrypter", "NoSuchPaddingException", true);
                    if (TextUtils.isEmpty(str2)) {
                        return str2;
                    }
                    return c(context, str);
                } catch (UnsupportedEncodingException e6) {
                    str2 = stringBuffer;
                    e.d("HwIdEncrypter", "UnsupportedEncodingException", true);
                    if (TextUtils.isEmpty(str2)) {
                        return c(context, str);
                    }
                    return str2;
                }
            }
        } catch (InvalidKeyException e7) {
            e.d("HwIdEncrypter", "InvalidKeyException", true);
            if (TextUtils.isEmpty(str2)) {
                return str2;
            }
            return c(context, str);
        } catch (BadPaddingException e8) {
            e.d("HwIdEncrypter", "BadPaddingException", true);
            if (TextUtils.isEmpty(str2)) {
                return c(context, str);
            }
            return str2;
        } catch (IllegalBlockSizeException e9) {
            e.d("HwIdEncrypter", "IllegalBlockSizeException", true);
            if (TextUtils.isEmpty(str2)) {
                return str2;
            }
            return c(context, str);
        } catch (NoSuchAlgorithmException e10) {
            e.d("HwIdEncrypter", "NoSuchAlgorithmException", true);
            if (TextUtils.isEmpty(str2)) {
                return c(context, str);
            }
            return str2;
        } catch (NoSuchPaddingException e11) {
            e.d("HwIdEncrypter", "NoSuchPaddingException", true);
            if (TextUtils.isEmpty(str2)) {
                return str2;
            }
            return c(context, str);
        } catch (UnsupportedEncodingException e12) {
            e.d("HwIdEncrypter", "UnsupportedEncodingException", true);
            if (TextUtils.isEmpty(str2)) {
                return c(context, str);
            }
            return str2;
        }
        if (TextUtils.isEmpty(str2)) {
            return c(context, str);
        }
        return str2;
    }

    public static void a(Context context) {
        a.a(context);
    }

    public static String b(Context context, String str) {
        if (VERSION.SDK_INT >= 23) {
            return d.a(context.getPackageName() + ".hwidsdk", str);
        }
        return a.a(context, str);
    }

    public static String c(Context context, String str) {
        String str2;
        int parseInt;
        try {
            parseInt = Integer.parseInt(g.b(context, "encryptversion"));
        } catch (Exception e) {
            e.b("HwIdEncrypter", "get version of encrypted is null, use GRADE_VERSION_KEYSTORE directory", true);
            parseInt = 3;
        }
        if (VERSION.SDK_INT < 23 || r0 < 3) {
            str2 = null;
        } else {
            str2 = d.b(context.getPackageName() + ".hwidsdk", str);
        }
        if (TextUtils.isEmpty(str2)) {
            return a.b(context, str);
        }
        return str2;
    }

    public static PublicKey a(String str) throws Exception {
        return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Hex.decode(str)));
    }

    private static String a() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(HwAccountConstants.ENCODE_STR_S).append(Hex.PART_KEY_CODE_S).append("E4BEE2B4E6161735A0B0CD276212B06FFCF451E7C48DD3D18B5E8FED65F59FC180DDB6DDA0EBFA1F").append("C5C4C268462301C4B98B2C59E1C0BA7657BAEC5744FB04A2CA108755CF56395F594F7B42265D72BB0596355D41F27EA3707DFD04B4D38BAE5EBB9D2080CEA40CCCC9DDF8030EBCAA793A9CBC8C05D4D4D4B4C6535738B9FE5A87C76EF471A0E11605A8E112FE8DB329F3BC27937208B4C51745E7A5FBC6E6F02519D31A12F40C93BBCFF8CD6E46214F12BD25B28C018E7BAC944646C0E1F37113038CBA656AFA5F47E19DF934C4073A431C9E6406C30478FEF43BE96B58B8688C7E20C63147D3D147EF13F3778A188BDB1A6A068F39A3882E3342247045859A029593A10A85A2B9E1626ECBF20E38DB2931B86DA7768B74F21FBBD793840A59A8B6D7FC570B01801F0F417C10BB213585847E57AE516EBB72AAA8A254278623CF87D193089B22547B079D556FCC738D0203010001");
        return stringBuilder.toString();
    }

    public static String b(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            Object encrypt = RSAEncrypt.encrypt(str, a(a()));
            if (!TextUtils.isEmpty(encrypt)) {
                return Hex.encode(Base64.decode(encrypt, 0));
            }
        } catch (Exception e) {
            e.d("HwIdEncrypter", "rsaEncrpter Exception", true);
        }
        return "";
    }
}
